
#include <stdio.h>
#include <unistd.h>

#include "ohos_init.h"
#include "cmsis_os2.h"
#include "wifiiot_gpio.h"
#include "wifiiot_gpio_ex.h"
#include "wifiiot_pwm.h"
#include "wifiiot_adc.h"
#include "wifiiot_i2c.h"
#include "wifiiot_errno.h"
#include "wifiiot_watchdog.h"
#include "wifi_hotspot.h"
#include "utils_file.h"
#include "wlan.h"

#include "ssd1306.h"

#define OLED_I2C_BAUDRATE 400*1000

#define ANALOG_KEY_CHAN_NAME WIFI_IOT_ADC_CHANNEL_2

osSemaphoreId_t g_keySem;

void PinConfig(void) {
    IoSetFunc(WIFI_IOT_IO_NAME_GPIO_13, WIFI_IOT_IO_FUNC_GPIO_13_I2C0_SDA);
    IoSetFunc(WIFI_IOT_IO_NAME_GPIO_14, WIFI_IOT_IO_FUNC_GPIO_14_I2C0_SCL);
    IoSetFunc(WIFI_IOT_IO_NAME_GPIO_5, WIFI_IOT_IO_FUNC_GPIO_5_GPIO);
}

void handlePressKeyTask(void* arg) {
    (void)arg;
    while(true) {
        osStatus_t status = osSemaphoreAcquire(g_keySem,65535);
        if(status != 0) {
            continue;
        }

        while(true) {
            unsigned short data = 0;
            AdcRead(ANALOG_KEY_CHAN_NAME, &data, WIFI_IOT_ADC_EQU_MODEL_4, WIFI_IOT_ADC_CUR_BAIS_DEFAULT, 0);
            float voltage = (float)data * 1.8 * 4 / 4096;

            if(voltage < 0.8f) {
                ssd1306_Fill(Black);
                ssd1306_SetCursor(0, 0);
                ssd1306_DrawString("Key1 Pressed!", Font_7x10, White);
                ssd1306_UpdateScreen();
            } else if(voltage < 2.0f) {
                ssd1306_Fill(Black);
                ssd1306_SetCursor(0, 0);
                ssd1306_DrawString("Key2 Pressed!", Font_7x10, White);
                ssd1306_UpdateScreen();
            } else {
                ssd1306_Fill(Black);
                ssd1306_SetCursor(0, 0);
                ssd1306_DrawString("Released!", Font_7x10, White);
                ssd1306_UpdateScreen();
                break;
            }
        }
    }
}

void KeyPressed(char *arg) {
    (void)arg;
    osSemaphoreRelease(g_keySem);
}

void InitKey(void) {
    GpioSetDir(WIFI_IOT_IO_NAME_GPIO_5, WIFI_IOT_GPIO_DIR_IN);
    GpioRegisterIsrFunc(WIFI_IOT_IO_NAME_GPIO_5, WIFI_IOT_INT_TYPE_EDGE, WIFI_IOT_GPIO_EDGE_FALL_LEVEL_LOW, KeyPressed, NULL);
    //GpioSetIsrMask(WIFI_IOT_IO_NAME_GPIO_5)
}

inline static void BoardInit(void) {
    GpioInit();
    PinConfig();
    I2cInit(WIFI_IOT_I2C_IDX_0, OLED_I2C_BAUDRATE);
    WatchDogDisable();
    ssd1306_Init();
    InitKey();
}


void AppMain(void) {
    BoardInit();

    ssd1306_SetCursor(0, 0);
    ssd1306_DrawString("Bootting...", Font_7x10, White);
    ssd1306_UpdateScreen();

    osSemaphoreAttr_t semAttr = {
        .name = "KeyPressSemi",
        .attr_bits = 0,
        .cb_mem = NULL,
        .cb_size = 0
    };
    g_keySem = osSemaphoreNew(8, 0, &semAttr);

    osThreadAttr_t attr;
    attr.name = "KeyHandleTask";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = 2048;
    attr.priority = osPriorityNormal;
    if (osThreadNew(handlePressKeyTask, NULL, &attr) == NULL) {
        printf("[%s]Falied to create KeyHandleTask!\n", __func__);
    }

    InitWLan();
}
APP_FEATURE_INIT(AppMain);